const Controller = require("../core/base");
const moment = require("moment");
const dbc = require('../DBConfig/config');

class TouristPlaceController extends Controller {
    async getRecommendList() {
        const { ctx } = this;
        const body = ctx.request.body;
        try {
            let condition = `type=${body.type}`;
            // 查询热门景点
            if(body.hot == 1) {
                condition = `hot=${body.hot}`
            }
            let sql = `select a.id,a.name,b.name as tagName,a.des,a.price,a.cover,a.hot,a.type,a.agent,c.comment_num,c.payee_num from tourist_place a LEFT JOIN tourist_place_tag b on a.tag=b.id left join tourist_comments c on a.id=c.tourist_place_id where ${condition}`;
            let query = await dbc.query(sql);

            this.success(query[0] || [])

        } catch(err) {
            this.fail(err);
        }
    }

    // 获取景点详情信息
    async getPlaceDetail() {
        const { ctx } = this;
        const body = ctx.request.body;

        try {
            let sql = `select a.*,b.banner,b.address,b.first_time,b.second_time,b.travel_detail from tourist_place a left join tourist_place_detail b on a.id=b.tourist_place_id where a.id=${body.id}`;
            let query = await dbc.query(sql);
            let data = query[0][0] || {};
            // 转换banner为数组
            if(data.banner) {
                data.banner = data.banner.split(",");
            }
            // 转换开园时间
            data.first_time = moment(data.first_time).format("HH:ss");
            data.second_time = moment(data.second_time).format("HH:ss");

            this.success(data);
        } catch(err) {
            this.fail(err);
        }
    }
}

module.exports = TouristPlaceController;